<?php
/**
*	@author SignpostMarv Martin
*	@package LSL
*/
define('LSL_NULL_KEY','00000000-0000-0000-0000-000000000000');
function LSL_is_uuid($string)
{
	if(is_string($string) === false)
	{
		return false;
	}
	else
	{
#	Crediting Tempus Heart for pointing out rather major logical flaw with regex pattern going from a-z.
		return (bool)preg_match('/^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/S',$string);
	}
}

function LSL_llXorBase64StringsCorrect($s1,$s2)
{
	if(strlen(base64_decode($s1)) > strlen(base64_decode($s2)))
	{
		$s2 = base64_encode(str_pad(base64_decode($s2),strlen(base64_decode($s1)),base64_decode($s2),STR_PAD_RIGHT));
	}
	return base64_encode(base64_decode($s1) ^ base64_decode($s2));
}

function LSL_llMD5String($string,$nonce=0)
{
	if(is_integer($nonce) === false)
	{
		throw new InvalidArgumentException('Nonce should be supplied as integer.');
	}
	return md5($string . ':' . $nonce);		
}

require_once('functions.php');
interface simple_401_tool
{
	public function __construct($hash,$key);
/**
*	@return string Message to return on a 401 error.
*/
	public function error401();
}
abstract class simple_401 implements simple_401_tool
{
/**
*	@param string $hash  The  hash generated by the originating script
*	@param string $key The key used by the originating script to generate the hash
*	@uses llMD5String()
*	@uses llXorBase64StringsCorrect()
**/
	public function __construct($hash,$key)
	{
		if(LSL_llMD5String(LSL_llXorBase64StringsCorrect(base64_encode($this->pass()),base64_encode($key)),0) !== $hash)
		{
			header('HTTP/1.1 401 Unauthorized');
			UHU_output($this->error401());
		}
	}
	protected function pass()
	{
		throw new RuntimeException('Code should not be using default pass.');
	}
}

abstract class LSL_helper_http_response
{
	const header_shard       = 'HTTP_X_SECONDLIFE_SHARD';

	const header_object_name = 'HTTP_X_SECONDLIFE_OBJECT_NAME';
	const header_object_key  = 'HTTP_X_SECONDLIFE_OBJECT_KEY';

	const header_region      = 'HTTP_X_SECONDLIFE_REGION';
	const header_local_pos   = 'HTTP_X_SECONDLIFE_LOCAL_POSITION';
	const header_local_rot   = 'HTTP_X_SECONDLIFE_LOCAL_ROTATION';
	const header_local_vel   = 'HTTP_X_SECONDLIFE_LOCAL_VELOCITY';

	const header_owner_name  = 'HTTP_X_SECONDLIFE_OWNER_NAME';
	const header_owner_key   = 'HTTP_X_SECONDLIFE_OWNER_KEY';

	public static function http_call_is_from_owner_prim($test_uid)
	{
		return (isset($_SERVER[self::header_owner_key]) && strtolower($test_uid) === strtolower($_SERVER[self::header_owner_key]));
	}
	public static function http_call_is_from_grid()
	{
		return isset(
			$_SERVER[self::header_shard],

			$_SERVER[self::header_object_name],
			$_SERVER[self::header_object_key],

			$_SERVER[self::header_region],
			$_SERVER[self::header_local_pos],
			$_SERVER[self::header_local_rot],
			$_SERVER[self::header_local_vel],

			$_SERVER[self::header_owner_name],
			$_SERVER[self::header_owner_key]
		);
	}
	public static function debug()
	{
		if(defined('UHU_DEBUG_MODE') && UHU_DEBUG_MODE === true)
		{
			$_SERVER['HTTP_X_SECONDLIFE_SHARD'] = '';

			$_SERVER['HTTP_X_SECONDLIFE_OBJECT_NAME'] = '';
			$_SERVER['HTTP_X_SECONDLIFE_OBJECT_KEY'] = '';

			$_SERVER['HTTP_X_SECONDLIFE_REGION'] = '';
			$_SERVER['HTTP_X_SECONDLIFE_LOCAL_POSITION'] = '';
			$_SERVER['HTTP_X_SECONDLIFE_LOCAL_ROTATION'] = '';
			$_SERVER['HTTP_X_SECONDLIFE_LOCAL_VELOCITY'] = '';
		}
		else
		{
			throw new RuntimeException('Cannot execute debug function when not in debug mode.');
		}
	}
}
?>